const { GraphQLServer } = require('graphql-yoga')
const { Prisma } = require('prisma-binding')

const Query = require('./resolvers/Query');
const Mutation = require('./resolvers/Mutation');
const Subscription = require('./resolvers/Subscription');
const Feed = require('./resolvers/Feed');

// const resolvers = {
//   Query: {
//     feed(parent, args, ctx, info) {
//       return ctx.db.query.posts({ where: { isPublished: true } }, info)
//     },
//     drafts(parent, args, ctx, info) {
//       return ctx.db.query.posts({ where: { isPublished: false } }, info)
//     },
//     post(parent, { id }, ctx, info) {
//       return ctx.db.query.post({ where: { id } }, info)
//     },
//   },
//   Mutation: {
//     createDraft(parent, { title, text }, ctx, info) {
//       return ctx.db.mutation.createPost(
//         {
//           data: {
//             title,
//             text,
//             isPublished: false,
//           },
//         },
//         info,
//       )
//     },
//     deletePost(parent, { id }, ctx, info) {
//       return ctx.db.mutation.deletePost({ where: { id } }, info)
//     },
//     publish(parent, { id }, ctx, info) {
//       return ctx.db.mutation.updatePost(
//         {
//           where: { id },
//           data: { isPublished: true },
//         },
//         info,
//       )
//     },
//   },
// }
const resolvers = {
  Query,
  Mutation,
  Subscription,
  Feed,
};

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      typeDefs: 'src/generated/prisma.graphql',
      endpoint: 'http://localhost:4466/graphql_node_basic/dev', // the endpoint of the Prisma DB service
      secret: 'mysecret123', // specified in database/prisma.yml
      debug: true, // log all GraphQL queryies & mutations
    }),
  }),
})

server.start(() => console.log('Server is running on http://localhost:4000'))
